package io.github.junxworks.qt.modules.mm.engine.functions;

import java.util.Map;

import io.github.junxworks.junx.core.exception.BaseRuntimeException;
import io.github.junxworks.junx.core.util.StringUtils;
import io.github.junxworks.junx.stat.Stat;
import io.github.junxworks.junx.stat.StatContext;
import io.github.junxworks.junx.stat.StatDefinition;
import io.github.junxworks.junx.stat.datawindow.DataBundle;
import io.github.junxworks.junx.stat.datawindow.DataWindowConstants;
import io.github.junxworks.junx.stat.datawindow.prioritywindow.Priorities;
import io.github.junxworks.junx.stat.function.FuncDef;
import io.github.junxworks.qt.constants.QtConstants;
import io.github.junxworks.qt.modules.data.vo.DStockProfileVo;
import io.github.junxworks.qt.modules.mm.engine.CtxHolder;
import io.github.junxworks.qt.modules.mm.engine.DataContext;
import io.github.junxworks.qt.modules.tushare.service.BaseInfoService;

public class Functions {

	public static Map<String, Object> getDatastream(String dsName) {
		DataContext ctx = CtxHolder.getContext();
		return ctx.getDs(dsName);
	}

	public static Object getStat(String statName) {
		DataContext ctx = CtxHolder.getContext();
		String column = ctx.getKey().getColumn();
		if (QtConstants.KEYWORD_TS_CODE.equalsIgnoreCase(column) && statName.startsWith(QtConstants.INDUSTRY_STAT_PREFIX)) {
			String tsCode = ctx.getKey().getValue();
			DStockProfileVo profile = BaseInfoService.profilesCache.get(tsCode);
			String industry = profile.getIndustry();
			if (StringUtils.isNull(industry)) {
				throw new BaseRuntimeException("Unkown industry");
			}
			return ctx.getExeContext().getStatValue(createKeyName(statName, industry));
		} else {
			String key = ctx.getKey().getValue();
			return ctx.getExeContext().getStatValue(createKeyName(statName, key));
		}
	}

	public static Object getExpRes(String expName) {
		DataContext ctx = CtxHolder.getContext();
		String key = ctx.getKey().getValue();
		return ctx.getExeContext().getExpressionResult(createKeyName(expName, key));
	}

	public static String createKeyName(String preffix, String key) {
		return preffix + "-" + key;
	}

	public static Double linearRegression(Object[] numbers) throws Exception {
		StatDefinition statModel = new StatDefinition();
		statModel.setDataWindowSize(numbers.length);
		statModel.setStatFunction(FuncDef.LINEAR_REGRESSION);
		statModel.setDataWindowType(DataWindowConstants.WIN_TYPE_PRIORITY);
		statModel.setDataWindowPriorityType(Priorities.FIFO.toString());
		Stat so = Stat.create(statModel);
		long timestamp = System.currentTimeMillis();
		for (int i = 0, len = numbers.length; i < len; i++) {
			so.compose(new DataBundle(timestamp + i * 1000, Double.valueOf(String.valueOf(numbers[i]))));
		}
		StatContext ctx = new StatContext();
		return Double.valueOf(String.valueOf(so.getValue(ctx)));
	}

	public static String industry() {
		DataContext ctx = CtxHolder.getContext();
		String tsCode = ctx.getKey().getValue();
		DStockProfileVo profile = BaseInfoService.profilesCache.get(tsCode);
		String industry = profile.getIndustry();
		return StringUtils.isNull(industry) ? "未知" : industry;
	}
}
